<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>webpack4配置详解 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="webpack,">
  

  <meta name="description" content="来源网络  一、entry//方式一：单文件写法entry: &amp;#123;	index: &apos;./src/pages/route.js&apos;,	//about: &apos;./src/pages/about.js&apos;,	//other:()=&amp;gt;&amp;#123;...&amp;#125;&amp;#125;//方式二：多文件写法entry: &amp;#123;	/*index:[		&apos;webpack-hot-middleware/c">
<meta name="keywords" content="webpack">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack4配置详解">
<meta property="og:url" content="http://blog.poetries.top/2018/11/18/webpack4-config/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="来源网络  一、entry//方式一：单文件写法entry: &amp;#123;	index: &apos;./src/pages/route.js&apos;,	//about: &apos;./src/pages/about.js&apos;,	//other:()=&amp;gt;&amp;#123;...&amp;#125;&amp;#125;//方式二：多文件写法entry: &amp;#123;	/*index:[		&apos;webpack-hot-middleware/c">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2018/11/10/166fb92aa53f846f">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2018/11/10/166fb92b5f33e92c">
<meta property="og:updated_time" content="2020-08-15T04:25:31.942Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="webpack4配置详解">
<meta name="twitter:description" content="来源网络  一、entry//方式一：单文件写法entry: &amp;#123;	index: &apos;./src/pages/route.js&apos;,	//about: &apos;./src/pages/about.js&apos;,	//other:()=&amp;gt;&amp;#123;...&amp;#125;&amp;#125;//方式二：多文件写法entry: &amp;#123;	/*index:[		&apos;webpack-hot-middleware/c">
<meta name="twitter:image" content="https://user-gold-cdn.xitu.io/2018/11/10/166fb92aa53f846f">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、entry"><span class="toc-text">一、entry</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、output"><span class="toc-text">二、output</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、hash"><span class="toc-text">三、hash</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、mode"><span class="toc-text">四、mode</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、devtool"><span class="toc-text">五、devtool</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、optimization"><span class="toc-text">六、optimization</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#七、resolve-配置模块如何解析"><span class="toc-text">七、resolve - 配置模块如何解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#八、module-rules-编译规则"><span class="toc-text">八、module.rules - 编译规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#九、项目中常用loader"><span class="toc-text">九、项目中常用loader</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十、plugins-插件"><span class="toc-text">十、plugins - 插件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十一、plugins-loader-区别"><span class="toc-text">十一、plugins/loader 区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十二、webpack-dev-server"><span class="toc-text">十二、webpack-dev-server</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十三、webpack4删除的点"><span class="toc-text">十三、webpack4删除的点</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-webpack4-config" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">webpack4配置详解</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.11.18</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.8k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 7分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <blockquote>
<p>来源网络</p>
</blockquote>
<h2 id="一、entry"><a href="#一、entry" class="headerlink" title="一、entry"></a>一、entry</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//方式一：单文件写法</span></span><br><span class="line">entry: &#123;</span><br><span class="line">	index: <span class="string">'./src/pages/route.js'</span>,</span><br><span class="line">	<span class="comment">//about: './src/pages/about.js',</span></span><br><span class="line">	<span class="comment">//other:()=&gt;&#123;...&#125;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//方式二：多文件写法</span></span><br><span class="line">entry: &#123;</span><br><span class="line">	<span class="comment">/*index:[</span></span><br><span class="line"><span class="comment">		'webpack-hot-middleware/client',</span></span><br><span class="line"><span class="comment">		'./src/root.js'</span></span><br><span class="line"><span class="comment">	],*/</span></span><br><span class="line">	index: [<span class="string">'./src/root.js'</span>],</span><br><span class="line">	vendors : [<span class="string">'react'</span>,<span class="string">'react-dom'</span>,<span class="string">'redux'</span>,<span class="string">'react-router'</span>,<span class="string">'classnames'</span>],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="二、output"><a href="#二、output" class="headerlink" title="二、output"></a>二、output</h2><ul>
<li><code>path</code>: 输出文件的目录，</li>
<li><code>filename</code>:输出的文件名，它一般跟你<code>entry</code>配置相对应，如：<code>js/[name].js</code> <code>name</code>在这里表示的是<code>[index、vendors]</code>，</li>
<li><code>chunkFilename</code>：块，配置了它，非入口<code>entry</code>的模块，会帮自动拆分文件，也就是大家常说的按需加载，与路由中的 <code>require.ensure</code>相互应</li>
<li><code>publicPath</code>：文件输出的公共路径，</li>
<li><code>pathinfo</code>：即保留相互依赖的包中的注释信息，这个基本不用主动设置它，它默认 - <code>development</code> 模式时的默认值是 <code>true</code>，而在 <code>production</code> 模式时的默认值是 <code>false</code>，</li>
<li>主要的就是这些，还有一些其他的<code>library</code>、<code>libraryTarget</code>、<code>auxiliaryComment</code>等</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">output: &#123;</span><br><span class="line">	path: path.resolve(__dirname, <span class="string">'../assets'</span>),</span><br><span class="line">	filename: <span class="string">'js/[name].js'</span>,</span><br><span class="line">	chunkFilename: <span class="string">'js/[name].[chunkhash:8].js'</span>,</span><br><span class="line">	publicPath: <span class="string">'/_static_/'</span>, <span class="comment">//最终访问的路径就是：localhost:3000/_static_/js/*.js</span></span><br><span class="line">	<span class="comment">//pathinfo:true,</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="三、hash"><a href="#三、hash" class="headerlink" title="三、hash"></a>三、hash</h2><blockquote>
<p>常用的有三种</p>
</blockquote>
<table>
<thead>
<tr>
<th>模板</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>hash</code></td>
<td>模块标识符的<code>hash</code>,一般应用于<code>filename：&#39;[name].[hash].js&#39;</code></td>
</tr>
<tr>
<td><code>chunkhash</code></td>
<td>按需加载块内容的<code>hash</code>，根据chunk自身的内容计算而来,<code>&#39;js/[name].[chunkhash:8].js&#39;</code></td>
</tr>
<tr>
<td><code>contenthash</code></td>
<td>这个没有用过，看了下文档它是在提取<code>css</code>文件时根据内容计算而来的 <code>hash</code> ，结合<code>ExtractTextWebpackPlugin</code>插件使用</td>
</tr>
<tr>
<td><code>hash</code>长度</td>
<td>默认<code>20</code>，可自定:<code>[hash:8]</code>、<code>[chunkhash:16]</code></td>
</tr>
</tbody>
</table>
<h2 id="四、mode"><a href="#四、mode" class="headerlink" title="四、mode"></a>四、mode</h2><ul>
<li>这个属于<code>webpack4</code>才新增的，4之前大家一般用<code>DefinePlugin</code>插件设置</li>
<li><code>mode：development`</code>，<code>production</code>，<code>none</code>，</li>
<li><code>development</code> : 开发模式，打包的代码不会被压缩，开启代码调试，</li>
<li><code>production</code> : 生产模式，则正好反之。</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">//方法一</span></span><br><span class="line">webpack --mode development/production</span><br><span class="line"></span><br><span class="line"><span class="comment">//方法二</span></span><br><span class="line">……</span><br><span class="line">mode:<span class="string">'development/production'</span></span><br><span class="line">……</span><br></pre></td></tr></table></figure>
<h2 id="五、devtool"><a href="#五、devtool" class="headerlink" title="五、devtool"></a>五、devtool</h2><ul>
<li>控制是否生成，以及如何生成 <code>source map</code>文件，开发环境下更有利于定位问题，默认 <code>false</code></li>
<li>当然它的开启，也会影响编译的速度，所以生产环境一定一定记得关闭</li>
<li>常用的值<ul>
<li><code>cheap-eval-source-map</code></li>
<li><code>eval-source-map</code></li>
<li><code>cheap-module-eval-source-map</code></li>
<li><code>inline-cheap-module-source-map</code>等等，</li>
<li>更详细的可以去<a href="https://webpack.js.org/concepts/" target="_blank" rel="noopener">官方查看</a></li>
</ul>
</li>
<li>一般使用：<code>eval-source-map</code>较多，每个都有它不一样的特性</li>
</ul>
<p><img src="https://user-gold-cdn.xitu.io/2018/11/10/166fb92aa53f846f" alt></p>
<h2 id="六、optimization"><a href="#六、optimization" class="headerlink" title="六、optimization"></a>六、optimization</h2><ul>
<li><code>optimization</code>是<code>webpack4</code>新增的，主要是用来让开发者根据需要自定义一些优化构建打包的策略配置</li>
<li><code>minimize：true/false</code>,告诉<code>webpack</code>是否开启代码最小化压缩</li>
<li><code>minimizer</code>：自定<code>js</code>优化配置，会覆盖默认的配置，结合<code>UglifyJsPlugin</code>插件使用</li>
<li><code>removeEmptyChunks: bool</code> 值，它检测并删除空的块。将设置为<code>false</code>将禁用此优化</li>
<li><code>nodeEnv</code>：它并不是<code>node</code>里的环境变量，设置后可以在代码里使用 <code>process.env.NODE_ENV === &#39;development&#39;</code>来判断一些逻辑，生产环境<code>UglifyJsPlugin</code>会自动删除无用代码</li>
<li><code>splitChunks</code> ：取代了<code>CommonsChunkPlugin</code>，自动分包拆分、代码拆分，详细默认默认配置，只会作用于异步加载的代码块 —— <code>chunks:&#39;async&#39;</code>，它有三个值：<code>all</code>,<code>async</code>,<code>initial</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//环境变更也可以直接 在启动中设置</span></span><br><span class="line"> <span class="comment">//webpack --env.NODE_ENV=local --env.production --progress</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//splitChunks 默认配置</span></span><br><span class="line">splitChunks: &#123;</span><br><span class="line">  chunks: <span class="string">'async'</span>,</span><br><span class="line">  minSize: <span class="number">30000</span>,</span><br><span class="line">  maxSize: <span class="number">0</span>,</span><br><span class="line">  minChunks: <span class="number">1</span>,</span><br><span class="line">  maxAsyncRequests: <span class="number">5</span>,</span><br><span class="line">  maxInitialRequests: <span class="number">3</span>,</span><br><span class="line">  automaticNameDelimiter: <span class="string">'~'</span>,</span><br><span class="line">  name: <span class="literal">true</span>,</span><br><span class="line">  cacheGroups: &#123;</span><br><span class="line">    vendors: &#123;</span><br><span class="line">      test: <span class="regexp">/[\\/]node_modules[\\/]/</span>,</span><br><span class="line">      priority: <span class="number">-10</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="keyword">default</span>: &#123;</span><br><span class="line">      minChunks: <span class="number">2</span>,</span><br><span class="line">      priority: <span class="number">-20</span>,</span><br><span class="line">      reuseExistingChunk: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><code>runtimeChunk</code>: 提取 <code>webpack</code>运行时代码,它可以设置为：<code>boolean</code>、<code>Object</code></li>
<li>该配置开启时，会覆盖 入口指定的名称</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">optimization: &#123;</span><br><span class="line">	runtimeChunk:<span class="literal">true</span>,<span class="comment">//方式一</span></span><br><span class="line">  runtimeChunk: &#123;</span><br><span class="line">    name: <span class="function"><span class="params">entrypoint</span> =&gt;</span> <span class="string">`runtimechunk~<span class="subst">$&#123;entrypoint.name&#125;</span>`</span> <span class="comment">//方式二</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://user-gold-cdn.xitu.io/2018/11/10/166fb92b5f33e92c" alt></p>
<h2 id="七、resolve-配置模块如何解析"><a href="#七、resolve-配置模块如何解析" class="headerlink" title="七、resolve - 配置模块如何解析"></a>七、resolve - 配置模块如何解析</h2><ul>
<li><code>extensions</code>：自动解析确定的扩展,省去你引入组件时写后缀的麻烦，</li>
<li><code>alias</code>：非常重要的一个配置，它可以配置一些短路径，</li>
<li><code>modules</code>：<code>webpack</code>解析模块时应该搜索的目录，<br>其他 <code>plugins</code>、<code>unsafeCache</code>、<code>enforceExtension</code>，基本没有怎么用到</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//extensions 后缀可以省略，</span></span><br><span class="line"><span class="keyword">import</span> Toast <span class="keyword">from</span> <span class="string">'src/components/toast'</span>; </span><br><span class="line"></span><br><span class="line"><span class="comment">// alias ,短路径</span></span><br><span class="line"><span class="keyword">import</span> Modal <span class="keyword">from</span> <span class="string">'../../../components/modal'</span> </span><br><span class="line"><span class="comment">//简写</span></span><br><span class="line"><span class="keyword">import</span> Modal <span class="keyword">from</span> <span class="string">'src/components/modal'</span> </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">resolve: &#123;</span><br><span class="line">  extensions: [<span class="string">'.js'</span>, <span class="string">'.jsx'</span>,<span class="string">'.ts'</span>,<span class="string">'.tsx'</span>, <span class="string">'.scss'</span>,<span class="string">'.json'</span>,<span class="string">'.css'</span>],</span><br><span class="line">  alias: &#123;</span><br><span class="line">    src :path.resolve(__dirname, <span class="string">'../src'</span>),</span><br><span class="line">    components :path.resolve(__dirname, <span class="string">'../src/components'</span>),</span><br><span class="line">    utils :path.resolve(__dirname, <span class="string">'../src/utils'</span>),</span><br><span class="line">  &#125;,</span><br><span class="line">  modules: [<span class="string">'node_modules'</span>],</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="八、module-rules-编译规则"><a href="#八、module-rules-编译规则" class="headerlink" title="八、module.rules - 编译规则"></a>八、module.rules - 编译规则</h2><ul>
<li><code>rules</code>：也就是之前的<code>loaders</code>，</li>
<li><code>test</code> ： 正则表达式，匹配编译的文件，</li>
<li><code>exclude</code>：排除特定条件，如通常会写<code>node_modules</code>，即把某些目录<code>/</code>文件过滤掉，</li>
<li><code>include</code>：它正好与<code>exclude</code>相反，</li>
<li><code>use -loader</code> ：必须要有它，它相当于是一个 <code>test</code> 匹配到的文件对应的解析器，<code>babel-loader</code>、<code>style-loader</code>、<code>sass-loader</code>、<code>url-loader</code>等等，</li>
<li><code>use - options</code>：它与<code>loader</code>配合使用，可以是一个字符串或对象，它的配置可以直接简写在<code>loader</code>内一起，它下面还有<code>presets</code>、<code>plugins</code>等属性</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="built_in">module</span>: &#123;</span><br><span class="line">	rules: [</span><br><span class="line">		&#123;</span><br><span class="line">			test: <span class="regexp">/\.(js|jsx)$/</span>,</span><br><span class="line">			exclude: <span class="regexp">/node_modules/</span>,</span><br><span class="line">			use: [</span><br><span class="line">				&#123;</span><br><span class="line">					loader: <span class="string">'babel-loader'</span>,</span><br><span class="line">					options: &#123;</span><br><span class="line">						presets: [</span><br><span class="line">							[<span class="string">'env'</span>,</span><br><span class="line">							&#123;</span><br><span class="line">								targets: &#123;</span><br><span class="line">								browsers: CSS_BROWSERS,</span><br><span class="line">							&#125;,</span><br><span class="line">						&#125;],<span class="string">'react'</span>, <span class="string">'es2015'</span>, <span class="string">'stage-0'</span></span><br><span class="line">						],</span><br><span class="line">						plugins: [</span><br><span class="line">							<span class="string">'transform-runtime'</span>,</span><br><span class="line">							<span class="string">'add-module-exports'</span>,</span><br><span class="line">						],</span><br><span class="line">					&#125;,</span><br><span class="line">				&#125;,</span><br><span class="line">			],</span><br><span class="line">		&#125;,</span><br><span class="line">		&#123;</span><br><span class="line">			test: <span class="regexp">/\.(scss|css)$/</span>,</span><br><span class="line">			use: [</span><br><span class="line">				<span class="string">'style-loader'</span>,</span><br><span class="line">				&#123;<span class="attr">loader</span>: <span class="string">'css-loader'</span>,<span class="attr">options</span>:&#123;<span class="attr">plugins</span>: [<span class="built_in">require</span>(<span class="string">'autoprefixer'</span>)(&#123;<span class="attr">browsers</span>: CSS_BROWSERS,&#125;),],<span class="attr">sourceMap</span>: <span class="literal">true</span>&#125;&#125;,</span><br><span class="line">				&#123;<span class="attr">loader</span>: <span class="string">'postcss-loader'</span>,<span class="attr">options</span>:&#123;<span class="attr">plugins</span>: [<span class="built_in">require</span>(<span class="string">'autoprefixer'</span>)(&#123;<span class="attr">browsers</span>: CSS_BROWSERS,&#125;),],<span class="attr">sourceMap</span>: <span class="literal">true</span>&#125;&#125;,</span><br><span class="line">				&#123;<span class="attr">loader</span>: <span class="string">'sass-loader'</span>,<span class="attr">options</span>:&#123;<span class="attr">sourceMap</span>: <span class="literal">true</span>&#125;&#125;</span><br><span class="line">			]</span><br><span class="line">		&#125;,</span><br><span class="line">		&#123;</span><br><span class="line">			test: <span class="regexp">/\.(png|jpg|jpeg|gif)$/</span>,</span><br><span class="line">			exclude: <span class="regexp">/node_modules/</span>,</span><br><span class="line">			use: [</span><br><span class="line">				&#123;</span><br><span class="line">					loader: <span class="string">'url-loader?limit=12&amp;name=images/[name].[hash:8].[ext]'</span>,</span><br><span class="line">				&#125;,</span><br><span class="line">			],</span><br><span class="line">		&#125;,</span><br><span class="line">		&#123;</span><br><span class="line">			test: <span class="regexp">/\.(woff|woff2|ttf|eot|svg)$/</span>,</span><br><span class="line">			exclude: <span class="regexp">/node_modules/</span>,</span><br><span class="line">			use: [</span><br><span class="line">				&#123;</span><br><span class="line">					loader: <span class="string">'file-loader?name=fonts/[name].[hash:8].[ext]'</span>,</span><br><span class="line">				&#125;,</span><br><span class="line">			],</span><br><span class="line">		&#125;,</span><br><span class="line">	],</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="九、项目中常用loader"><a href="#九、项目中常用loader" class="headerlink" title="九、项目中常用loader"></a>九、项目中常用loader</h2><ul>
<li><code>babel-loader</code>、<code>awesome-typescript-loader</code> js*/ts编译，</li>
<li><code>css-loader</code>、<code>postcss-loader</code>、<code>sass-loader</code>、<code>less-loader</code>、<code>style-loader</code> 等<code>css</code>样式处理</li>
<li><code>file-loader</code>、<code>url-loader</code>、<code>html-loader</code>等图片<code>/svg/html</code>等的处理</li>
</ul>
<h2 id="十、plugins-插件"><a href="#十、plugins-插件" class="headerlink" title="十、plugins - 插件"></a>十、plugins - 插件</h2><ul>
<li><code>UglifyJsPlugin</code></li>
<li><code>HotModuleReplacementPlugin</code></li>
<li><code>NoEmitOnErrorsPlugin</code></li>
<li><code>HtmlWebPackPlugin</code></li>
<li><code>ExtractTextPlugin</code></li>
<li><code>PreloadWebpackPlugin</code></li>
</ul>
<h2 id="十一、plugins-loader-区别"><a href="#十一、plugins-loader-区别" class="headerlink" title="十一、plugins/loader 区别"></a>十一、plugins/loader 区别</h2><ul>
<li><code>loader</code>的作用在于解析文件，比如把<code>ES6</code>转换成<code>es5</code>,甚至<code>ES3</code>,毕竟还有万恶的IE嘛；把<code>Sass</code>、<code>Less</code>解析成<code>CSS</code>，给<code>CSS</code>自动加上兼容的前缀；对图片进行一个解析等等</li>
<li><code>plugins</code>对<code>loader</code>干的事情进行优化分类、提取精华(公共代码提取)、做压缩处理(<code>js/css/html</code>压缩)、输出指定的目录等</li>
</ul>
<h2 id="十二、webpack-dev-server"><a href="#十二、webpack-dev-server" class="headerlink" title="十二、webpack-dev-server"></a>十二、webpack-dev-server</h2><ul>
<li><code>contentBase</code> ：告诉服务(<code>dev server</code>)在哪里查找文件，默认不指定会在是当期项目根目录，</li>
<li><code>historyApiFallback</code>:可以是<code>boolean</code>、 <code>object</code>，默认响应的入口文件，包括<code>404</code>都会指向这里，<code>object</code>见下面示例</li>
<li><code>compress</code>：启用 <code>gzip</code> 压缩，</li>
<li><code>publicPath</code>：它其实就是 <code>output.publicPath</code>，当你改变了它，即会覆盖了<code>output</code>的配置，</li>
<li><code>stats</code>： 可以自定控制要显示的编译细节信息，</li>
<li><code>proxy</code>：它其实就是<code>http-proxy-middleware</code>，可以进行处理一些代理的请求</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//方式一：不配置方式二的内容</span></span><br><span class="line"> webpack-dev-server --config webpack/webpack.config.dev.js</span><br><span class="line"><span class="comment">//指定 端口： --port=8080 </span></span><br><span class="line"><span class="comment">//开启热更新：--hot</span></span><br><span class="line"><span class="comment">//gzip： --compress</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//方式二</span></span><br><span class="line">devServer : </span><br><span class="line">	contentBase:<span class="string">'./assets'</span>,</span><br><span class="line">	host: <span class="string">'0.0.0.0'</span>,</span><br><span class="line">	port: <span class="number">9089</span>,</span><br><span class="line">	publicPath: <span class="string">'/assets/'</span>,</span><br><span class="line">	historyApiFallback: &#123;</span><br><span class="line">		index: <span class="string">'/views/index.html'</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	匹配路径，进入不同的入口文件，首席填坑官∙苏南的专栏,公Z好：honeyBadger8</span></span><br><span class="line"><span class="comment">	rewrites: [</span></span><br><span class="line"><span class="comment">			&#123; from: /^\/$/, to: '/views/landing.html' &#125;,</span></span><br><span class="line"><span class="comment">			&#123; from: /^\/subpage/, to: '/views/subpage.html' &#125;,</span></span><br><span class="line"><span class="comment">			&#123; from: /./, to: '/views/404.html' &#125;</span></span><br><span class="line"><span class="comment">		]</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	compress: <span class="literal">true</span>,</span><br><span class="line">	noInfo: <span class="literal">true</span>,</span><br><span class="line">	inline: <span class="literal">true</span>,</span><br><span class="line">	hot: <span class="literal">true</span>,</span><br><span class="line">	stats: &#123;</span><br><span class="line">		colors: <span class="literal">true</span>,</span><br><span class="line">		chunks: <span class="literal">false</span></span><br><span class="line">	&#125;,</span><br><span class="line">	proxy:&#123;</span><br><span class="line">		<span class="string">'/mockApi'</span>: <span class="string">'https://easy-mock.com/project/5a0aad39eace86040263d'</span> ,<span class="comment">//请求可直接写成  /mockApi/api/login...</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="十三、webpack4删除的点"><a href="#十三、webpack4删除的点" class="headerlink" title="十三、webpack4删除的点"></a>十三、webpack4删除的点</h2><ul>
<li><code>module.loaders</code></li>
<li><code>NoErrorsPlugin</code></li>
<li><code>CommonsChunkPlugin</code></li>
<li><code>DefinePlugin</code></li>
<li><code>OccurenceOrderPlugin</code></li>
</ul>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/11/18/react-ssr-next-deploy/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/11/18/react-performance-optimization/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
